package com.cskaoyan.day24.tcp2;


import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("127.0.0.1", 8888);
        OutputStream out = socket.getOutputStream();
        String str = new Scanner(System.in).nextLine();
        int length = str.getBytes().length;
        DataOutputStream dataOutputStream = new DataOutputStream(out);
        dataOutputStream.writeLong(length);
        out.write(str.getBytes());
        System.out.println("客户端字符串已经写入成功");

        System.out.println("开始写入文件");
        File file = new File("zclient/a.txt");
        long fileLen = file.length();
        dataOutputStream.writeLong(fileLen);
        FileInputStream in = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        int count;
        while ((count = in.read(bytes)) != -1) {
            out.write(bytes, 0, count);
            out.flush();
        }
        out.write("end".getBytes());
        System.out.println("文件写入成功！");

        System.out.println("开始再次写入文件");

        in = new FileInputStream(file);
        while ((count = in.read(bytes)) != -1) {
            out.write(bytes, 0, count);
            out.flush();
        }
        out.write("end".getBytes());
        System.out.println("文件再次写入成功！");

        in.close();
        socket.close();

    }
}
